]> git.r.bdr.sh - rbdr/super-polarity/blobdiff - Super Polarity/ActorManager.cs
I have the worst commits ever.
[rbdr/super-polarity] / Super Polarity / ActorManager.cs
index bca71687f588d15f5d210c6facd99f835e4593e7..8cd265a432a06d10867bd6fdd3e86836a638eb94 100644 (file)
@@ -9,10 +9,14 @@ namespace SuperPolarity
 {
     static class ActorManager
     {
-        static List<Actor> Actors;
+
+        static SuperPolarity Game;
+        static int OutlierBounds;
+        static IList<Actor> Actors;
 
         static ActorManager()
         {
+            OutlierBounds = 100;
             Actors = new List<Actor>();
         }
 
@@ -28,6 +32,8 @@ namespace SuperPolarity
 
         static public void Update(GameTime gameTime) 
         {
+            CheckActors();
+            CheckOutliers();
             foreach (Actor actor in Actors)
             {
                 actor.Update(gameTime);
@@ -41,5 +47,76 @@ namespace SuperPolarity
                 actor.Draw(spriteBatch);
             }
         }
+
+        static void CheckActors()
+        {
+            for (var i = Actors.Count - 1; i >= 0; i--)
+            {
+                if (i >= Actors.Count) {
+                    i = Actors.Count - 1;
+                }
+                Actor actor = Actors[i];
+                for (var j = i - 1; j >= 0; j--)
+                {
+                    Actor other = Actors[j];
+
+                    CheckCollision(actor, other);
+
+                    if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
+                    {
+                        CheckMagnetism((Ship)actor, (Ship)other);
+                    }
+                }
+            }
+        }
+
+        static void CheckCollision(Actor actor, Actor other)
+        {
+            var collision = Rectangle.Intersect(actor.Box, other.Box);
+            var inverseCollision = Rectangle.Intersect(other.Box, actor.Box);
+            if (!collision.IsEmpty && !actor.Dying && !other.Dying)
+            {
+                actor.Collide(other, collision);
+                other.Collide(actor, inverseCollision);
+            }
+
+        }
+
+        static void CheckMagnetism(Ship actor, Ship other)
+        {
+            if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral)
+            {
+                var dy = other.Position.Y - actor.Position.Y;
+                var dx = other.Position.X - actor.Position.X;
+                var linearDistance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
+                var angle = (float) Math.Atan2(dy, dx);
+                var otherAngle = (float)Math.Atan2(-dy, -dx);
+
+                if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius)
+                {
+                    actor.Magnetize(other, (float)linearDistance, angle);
+                    other.Magnetize(actor, (float)linearDistance, otherAngle);
+                }
+            }
+        }
+
+        static void CheckOutliers()
+        {
+            for (var i = Actors.Count; i > 0; i--)
+            {
+                var actor = Actors[i-1];
+                if (actor.Position.X < -OutlierBounds || actor.Position.Y < -OutlierBounds ||
+                    actor.Position.X > Game.GraphicsDevice.Viewport.Width + OutlierBounds ||
+                    actor.Position.Y > Game.GraphicsDevice.Viewport.Height + OutlierBounds)
+                {
+                    CheckOut(actor);
+                }
+            }
+        }
+
+        internal static void SetGame(SuperPolarity game)
+        {
+            Game = game;
+        }
     }
 }